const { findAllGoods, FindOneGoods } = require('../service/goods.service')

class Goods {
  async FindAll(ctx) {
    try {
      const { pageNum = 1, pageSize = 9, } = ctx.request.query
      console.log(ctx.request.query)
      const { type, category, sreach, sort, minPrice = 0, maxPrice = 1000 } = ctx.request.body
      const res = await findAllGoods(pageNum, pageSize, type, category, sreach, sort, minPrice, maxPrice)
      ctx.body = {
        code: 0,
        message: '请求数据成功',
        result: res
      }
    }
    catch (err) {
      console.error(err)
      ctx.body = {
        code: 10201,
        message: '请求数据失败',
        result: ''
      }
    }
  }
  async FindOne(ctx) {
    try {
      const id = ctx.request.params.id
      const res = await FindOneGoods(id)
      ctx.body = {
        code: 0,
        message: '请求数据成功',
        result: res
      }
    }
    catch (err) {
      console.error(err)
      ctx.body = {
        code: 10201,
        message: '请求数据失败',
        result: ''
      }
    }
  }
}

module.exports = new Goods